#include "StdAfx.h"
#include "mycallback.h"
#include <QDebug>

/// instance callback constructor
MyInstancesCallback::MyInstancesCallback(IrmMain *irmMain, DcmSeries *parentSeries, COphies* pNotify, QObject *parent)
    : DcmInstancesCallback(parent),
      irmMain(irmMain),
      parentSeries(parentSeries)
{
	m_pNotify = pNotify;
}

/// instance object handler
void MyInstancesCallback::myObjectHandler(DataModel *instance)
{
    // pass
    // dynamic_cast<DcmInstance*>(instance)
}

/// instance list handler
void MyInstancesCallback::myListHandler(QList<DataModel *> *list)
{
    qDebug() << "myobject handler for DcmInstance list called";

    if (list == NULL)
    {
        qDebug() << "list object is null. ";
        return;
    }

    for(unsigned int i = 0; i < list->size(); i++)
    {
        parentSeries->appendInstance(dynamic_cast<DcmInstance*>(list->at(i)));
    }

    /**
     *
     *
     *
     * UI JOB ??
     *
     *
     *
     */


    /**
     *
     *
     *
     * showing dcm ??
     *
     *
     *
     */

    // get parent uid
    // get amazon readonly url
    // and displaying?


}


////////////////////////////////////////////////////////////////////////////////////////////


/// series callback constructor
MySeriesCallback::MySeriesCallback(IrmMain *irmMain, DcmStudy *parentStudy, COphies* pNotify, QObject *parent)
    : DcmSeriesCallback(parent),
      irmMain(irmMain),
      parentStudy(parentStudy),
      instancesCallbacks(new QList<MyInstancesCallback*>())
{
	m_pNotify = pNotify;
}

/// series object handler
void MySeriesCallback::myObjectHandler(DataModel *series_shadow)
{
    DcmSeries* series = dynamic_cast<DcmSeries*>(series_shadow);
    qDebug() << "myobject handler for DcmSeries called";

    if(series == NULL)
    {
        qDebug() << "DcmSeries object is null";
        return;
    }

    // searching for instances
    if(series)
    {
        SearchDcmInstances search;
        search.set_dcm_series_key(series->get_dcm_series_key());

        MyInstancesCallback *instanceCallback = new MyInstancesCallback(irmMain, series, parent());
        instancesCallbacks->append(instanceCallback);
        irmMain->listDcmInstances(instanceCallback, search);
    } else
    {
        qDebug() << "error;";
    }

    /**
     *
     *
     *
     * UI JOB ??
     *
     *
     *
     */

}

/// series list handler
void MySeriesCallback::myListHandler(QList<DataModel *> *list)
{
	if( list == NULL && m_pNotify == NULL )
		return;

	m_pNotify->GetSeriesList( list )


#if (0)
    qDebug() << "myobject handler for series list called";

    if (list == NULL)
    {
        qDebug() << "list object is null. ";
        return;
    }

    // append to parent study
    for(unsigned int i = 0; i < list->size(); i++)
    {
        parentStudy->appendSeries(dynamic_cast<DcmSeries*>(list->at(i)));

        if(true) // if auto
        {
            DcmSeries* series = dynamic_cast<DcmSeries*>(list->at(i));
            SearchDcmInstances search;
            search.set_dcm_series_key(series->get_dcm_series_key());

            MyInstancesCallback *instanceCallback = new MyInstancesCallback(irmMain, series, parent());
            instancesCallbacks->append(instanceCallback);
            irmMain->listDcmInstances(instanceCallback, search);
        }
    }

    /**
     *
     *
     *
     * UI JOB ??
     *
     *
     *
     */
#endif
}


////////////////////////////////////////////////////////////////////////////////////////////


MyStudiesCallback::MyStudiesCallback(IrmMain* irmMain, COphies* pNotify, QObject* parent)
    : DcmStudiesCallback(parent),
      irmMain(irmMain),
      seriesCallbacks(new QList<MySeriesCallback*>())
{
	m_pNotify = pNotify;
}


void MyStudiesCallback::myObjectHandler(DataModel *study_shadow)
{
    DcmStudy* study = dynamic_cast<DcmStudy*>(study_shadow);
    qDebug() << "myobject handler for DcmStudy called";

    if (study == NULL)
    {
        qDebug() << "DcmStudy object is null. ";
        return;
    }

    // searching for series
    if(study)
    {
        SearchDcmSeries search;
        search.set_dcm_study_key(study->get_dcm_study_key());

        MySeriesCallback *seriesCallback = new MySeriesCallback(irmMain, study, parent());
        seriesCallbacks->append(seriesCallback);
        irmMain->listDcmSeries(seriesCallback, search);
    } else
    {
        qDebug() << "error; could not cast to DcmStudy";
    }
}

void MyStudiesCallback::myListHandler(QList<DataModel *> *list)
{
    qDebug() << "my list handler for DcmStudy";

    /**
     *
     *
     *
     * UI JOB ??
     *
     *
     *
     */

}
























